import java.util.PriorityQueue;

public class SmallestK {


    public int[] smallestK(int[] arr, int k) {
        int[] ret = new int[k];
        if(arr == null || k <= 0) {
            return ret;
        }
        PriorityQueue<Integer> pq = new PriorityQueue<>((a,b) -> b-a); //构建大堆
        for(int i = 0; i < k; ++i) {
            pq.offer(arr[i]);  //先进K个数构建大堆
        }

        for(int i = k; i < arr.length; ++i) {
            if(pq.peek() > arr[i]) {
                pq.poll();
                pq.offer(arr[i]);
            }
        }
        for(int i = 0; i < k; ++i) {
            ret[i] = pq.poll();
        }
        return ret;
    }


}
